package v3.chapter4.lockMethodTest1;

import java.util.concurrent.locks.ReentrantLock;

public class Run2 {

	public static void main(String[] args) throws InterruptedException {
		final Service2 service = new Service2();

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				service.serviceMethod1();
			}
		};

		Thread[] threadArray = new Thread[10];
		for (int i = 0; i < 10; i++) {
			threadArray[i] = new Thread(runnable);
		}
		for (int i = 0; i < 10; i++) {
			threadArray[i].start();
		}
		Thread.sleep(2000);
		
		System.out.println("有线程数：" + service.lock.getQueueLength() + "在等待获取锁！");
	}

}


class Service2{
	public ReentrantLock lock=new ReentrantLock();
	
	public void serviceMethod1() {
		try {
			lock.lock();
			System.out.println("ThreadName=" + Thread.currentThread().getName() + "进入方法！");
			Thread.sleep(Integer.MAX_VALUE);
		}catch(InterruptedException e) {
			e.printStackTrace();
		}finally {
			lock.unlock();
		}
	}
}